<?php
/**
 *  优惠券类
 */
namespace Common\Model;

use Think\Model;

class CouponModel extends Model{
	public $coupon_type=array(1=>"vip",2=>"all",3=>"old");
	/**
	 * 优惠券新增
	 * @param unknown $type 优惠券种类
	 * @param unknown $money 优惠券金额
	 * @param unknown $uid   微信id
	 * @param unknown $pid   父微信id
	 */
	public function create($uid,$pid,$channelNo,$type=1){
		$param['uid']=$uid;
		$param['channelNo']=channelNo;
		//$param['money']=$money;
		$param['type']=$type;
		$param['createtime']=date("Y-m-d H:i:s",time());
		$param['status']=0;
		$r=M("coupon")->add($param);
		if($r){
			return 1;
		}else{
			return 0;
		}
	}
	/**
	 * //新增用户送优惠券
	 * @param unknown $uid
	 * @param unknown $pid
	 * @param unknown $money
	 */
	public function inviteAdd($uid,$channelNo){
		
		
		
		//获取父类的优惠券金额
		$map['channelNo']=$channelNo;
		$puser=M("partner")->where($map)->find();
		if($puser){
			//$money=$puser['couponMoney'];
		}
		//判断是否已经获取邀请优惠券
		$map=array();
		$map['type']=1;
		$map['uid']=$uid;
		$user=M("coupon")->where($map)->find();
		if($user){
			return 0;
		}
		$r=$this->create($uid,$pid,$channelNo);
		return $r;
	}
	/**
	 * 获取当前人的优惠券
	 * uid 用户
	 * cid 课程
	 */
	public function getInviteCoupon($uid,$cid){
		$map=array();
		$map['uid']=$uid;
		$map['type']=1;
		$map['status']=0;
		
		$coupon=M("coupon")->where($map)->find();
		
		if($coupon){
			$partner=D("Student")->getStudentChannel($uid);
			
			$course=M("course")->find($cid);
			$str="preferential_".$this->coupon_type[$course['level']];
			$coupon['money']=$partner[$str];
		}
		if($coupon){
			return $coupon;
		}else{
			return false;
		}
	}
	//优惠券使用
	
	//优惠券删除
		
}